var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({
  data: {
    imgUrl: globalData.imgUrl,
    titleFlag: 0,
    titleList: [],
    pageNo: 1,
    pageSize: 10,
    totalPage: 1,//总页数
    list:[],
  },
  //类型
  types: function () {
    var that = this;
    var params = new Object();
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/coupon/getCouponTypeList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("类型：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data;
          that.setData({ titleList: rows, titleFlag:rows[0].id})
          that.list();
        }
      }
    })
  },
  
  list: function () {
    var params = new Object();
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    params.couponType = this.data.titleFlag;
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/coupon/getCouponList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("res：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          if (rows != null && rows != ""){
            for (let i = 0; i < rows.length; i++) {
              if (rows[i].limitDescribe.length > 18){
                rows[i].limitDescribe = rows[i].limitDescribe.substring(0,18)+"...";
              }
            }
          }
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNo == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        }
      }
    })
  },
  //设置显示块id
  titleSelect: function (e) {
    let id = e.currentTarget.id;
    this.setData({
      titleFlag: id,
      pageNo: 1,
    })
    this.list();
  },
  //领取券
  take:function(e){
    let index = e.currentTarget.id;
    let list = this.data.list;
    var that = this;
    var params = new Object();
    params.couponCode = list[index].coupon_code;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId },
      url: httpsUrl + "/coupon/takeCoupon",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("领取券：" + JSON.stringify(res));
        if (res.result == 0) {
          wx.showToast({title: '领取成功！',})
          //list[index].user_coupon_status = 1;//1,未核销/2,已核销/3,过期  无值则未领取
          //that.setData({list:list})
          that.list();
        }
      }
    })
  },
  content: function () {
    wx.setStorageSync("contentsType", 3);
    wx.navigateTo({
      url: '../contents/contents',
    })
  },
  share:function(e){
    let userCouponId = e.currentTarget.id;
    wx.navigateTo({
      url: '../voucherShare/voucherShare?userCouponId=' + userCouponId,
    })
  },
  onLoad: function (options) {
    this.types();
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    pageNo = pageNo + 1;
    if (pageNo > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNo: pageNo })
      this.list();
    }
  },
  
})